package cn.tedu.vrd.filter;

import cn.tedu.vrd.vo.User;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "MyFilter",urlPatterns = {"/send.html","/admin.html","/product/delete"})

public class MyFilter implements Filter {
    //过滤器初始化时执行的方法
    public void init(FilterConfig config) throws ServletException {
    }


    public void destroy() { //过滤器销毁时执行的方法
    }

    //执行Controller方法之前和之后执行的方法
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request1 = (HttpServletRequest)request;
        HttpServletResponse response1 = (HttpServletResponse)response;
        //得到Session会话对象
        HttpSession session = request1.getSession();
        User u = (User) session.getAttribute("u");
        if (u!=null){//登录了
            chain.doFilter(request, response);
        }else { //让客户端跳转到登录页面
             response1.sendRedirect("/login.html");
        }

    }
}


